//
//  RecordViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/24.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "RecordViewController.h"
#import "RecordCell.h"

#import "MyOrderModel.h"

@interface RecordViewController () <UITableViewDataSource,UITableViewDelegate,DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>

@end

@implementation RecordViewController
{
    DOPDropDownMenu *_chooseView;
    UITableView *_tableView;
    
    NSArray *_menuTitle;
    NSArray *_firMenu;
    NSArray *_secMenu;
    NSArray *_thirMenu;
    NSArray *_fourMenu;
    
    TeacherModel *_teacherModel;
    MemberModel *_memberModel;
    
    MyOrderModel *_myOrderModel;
    NSInteger _pageIndex;
    NSMutableArray *_dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"上课记录"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    
    [self creatChooseView];
    [self creatTableView];
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
}

- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}

- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"t_member_Id":_memberModel.member_Id,
                            @"promulgator_mark":@"1",
                            @"pageIndex":[NSString stringWithFormat:@"%ld",_pageIndex],
                            @"pageSize":@"10",
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(ORDER_P) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [MyOrderModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"MyOrderData",
                         };
            }];
            
            if (_dataArray) {
                if (_tableView.mj_header.isRefreshing) {
                    [_dataArray removeAllObjects];
                }
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            _myOrderModel = [[MyOrderModel alloc] init];
            _myOrderModel = [MyOrderModel objectWithKeyValues:result];
            
            [_dataArray addObjectsFromArray:_myOrderModel.data];
            [_tableView reloadData];
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }else{
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }
    }];
}

- (void)creatChooseView
{
    
    _menuTitle = @[@"上课方式",@"课程类别",@"课程价格",@"选择区域"];
    _firMenu = @[@"上课方式",@"老师上门",@"学生上门",@"双方协商",@"在线授课"];
    _secMenu = @[@"课程类别"];
    _thirMenu = @[@"课程价格"];
    _fourMenu = @[@"选择区域"];

    _chooseView = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:40];
    _chooseView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    _chooseView.delegate = self;
    _chooseView.dataSource = self;
    [self.view addSubview:_chooseView];
}


- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return 4;
}


- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    if (column == 0) {
        return _firMenu.count;
    }else if(column == 1){
        return _secMenu.count;
    }else if(column == 2){
        return _thirMenu.count;
    }else if(column == 3){
        return _fourMenu.count;
    }else{
        return 0;
    }
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    switch (indexPath.column) {
        case 0: return _firMenu[indexPath.row];
            break;
        case 1: return _secMenu[indexPath.row];
            break;
        case 2: return _thirMenu[indexPath.row];
            break;
        case 3: return _fourMenu[indexPath.row];
            break;

        default:
            return nil;
            break;
    }
}

- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    
}


- (void)creatTableView
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, ECSCREEN_W, ECSCREEN_H - 109) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.backgroundColor = BG_COLOR;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

    [self.view addSubview:_tableView];
}


- (void)headerRefresh
{
    _pageIndex = 0;
    [self creatHttpRequest];
}

- (void)footerRefresh
{
    if (_dataArray.count%10  == 0) {
        _pageIndex++;
        [self creatHttpRequest];
    }else{
        [_tableView.mj_footer endRefreshingWithNoMoreData];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 5)];
    view.backgroundColor = BG_COLOR;
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"recordCell";
    RecordCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"RecordCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[RecordCell class]]) {
                cell = (RecordCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
